angular.module(moduleName + '.controllers')
  .controller('InfoEntryCtrl', function($scope, VALIDATION, $rootScope) {
    var infoEntry = this;


    // 获取所选客户
    $rootScope.$on('send-result', function(event, data) {
      var user = data.result;
      if (user && user.name) {
        infoEntry.preName = user.name;
        infoEntry.name = user.name; // 姓名
        infoEntry.age = user.age; // 年龄
        infoEntry.insureNationality = user.nationality; // 国籍
        infoEntry.certType = user.certType; // 证件类型
        infoEntry.certNo = user.certNo; //证件号码
        infoEntry.email = user.email; // 邮箱
        infoEntry.birthday = new Date(user.birthday); // 出生日期
        infoEntry.gender = user.gender; // 性别
        infoEntry.certExpireType = user.certExpireType; //证件有效期
        infoEntry.certExpireDate = new Date(user.certExpireDate); //证件失效日期
        infoEntry.address = user.address; // 详细地址
        infoEntry.zipcode = user.zipcode; // 邮编号码
        infoEntry.inhabitantType = user.inhabitantType; // 户口
        infoEntry.money = user.money; // 年收入
        infoEntry.send = user.send; // 电子保单形式
        infoEntry.profession = user.profession; // 职业
        infoEntry.cityType = user.cityType;
        infoEntry.areaType = user.areaType;
      } else {
        // 性别默认男
        infoEntry.gender = 1;
        // 国籍默认中国
        infoEntry.insureNationality = "0156";
        // 证件类型默认身份证
        infoEntry.certType = "A";
        // 证件类型默认长期有效
        infoEntry.certExpireType = 1;
        // 证件类型默城镇
        infoEntry.inhabitantType = "T";
        // 默认电子保单
        infoEntry.send = 1;
        //默认为专业技术人员
        infoEntry.profession = 1;
        //默认为上海市
        infoEntry.cityType = 1;
        //默认为上海市
        infoEntry.areaType = 1;

      }
    });

    infoEntry.insureNationalities = [{
      label: '中国',
      value: '0156'
    }, {
      label: '中国香港',
      value: '0344'
    }, {
      label: '中国台湾',
      value: '0158'
    }, {
      label: '中国澳门',
      value: '0446'
    }, {
      label: '其它地区',
      value: '0999'
    }];

    infoEntry.certTypes = [{
      value: "A",
      label: "身份证"
    }, {
      value: "G",
      label: "港澳台通行证"
    }, {
      value: "I",
      label: "护照"
    }];


    infoEntry.inhabitantTypes = [{
      value: "T",
      label: "城镇"
    }, {
      value: "C",
      label: "农村"
    }];

    infoEntry.certExpireTypes = [{
      value: 1,
      label: "长期有效"
    }, {
      value: 2,
      label: "有效期"
    }];

    infoEntry.genders = [{
      value: 1,
      label: "男"
    }, {
      value: 2,
      label: "女"
    }];

    //保单的寄送方式
    infoEntry.sends = [{
      value: 1,
      label: "电子保单"
    }, {
      value: 2,
      label: "纸质保单"
    }];


    //市
    infoEntry.cityTypes = [{
      value: 1,
      label: "上海市"
    }];


    //区
    infoEntry.areaTypes = [{
      value: 1,
      label: "徐汇区"
    }, {
      value: 2,
      label: "闵行区"
    }, {
      value: 3,
      label: "浦东新区"
    }, {
      value: 4,
      label: "普陀区"
    }];


    //职业
    infoEntry.professions = [{
      value: 1,
      label: "专业技术人员"
    }, {
      value: 2,
      label: "行政人员"
    }];

    //与被保人关系
    infoEntry.relations = [{
      value: 1,
      label: "本人"
    }, {
      value: 2,
      label: "配偶"
    }, {
      value: 3,
      label: "父母"
    }, {
      value: 4,
      label: "子女"
    }];

    // 投保人年龄限制
    infoEntry.minBir = new Date('1965-09-10');
    infoEntry.maxBir = new Date('2017-04-25');

    //默认为本人
    infoEntry.relation = 1;
    // 切换显示基本信息
    infoEntry.showBasicInfo = true;
    infoEntry.showBasic = function() {
      if (infoEntry.showBasicInfo == true) {
        infoEntry.showBasicInfo = false;
        $('.basic-info').slideUp(200);
      } else {
        infoEntry.showBasicInfo = true;
        $('.basic-info').slideDown(200);
      }
    };
    // 切换显示地址信息
    infoEntry.showAddInfo = true;
    infoEntry.showAdd = function() {
      if (infoEntry.showAddInfo == true) {
        infoEntry.showAddInfo = false;
        $('.addr-info').slideUp(200);
      } else {
        infoEntry.showAddInfo = true;
        $('.addr-info').slideDown(200);
      }
    };
    // 切换显示关系信息
    infoEntry.showRelation = true;
    infoEntry.showRelationInfo = function() {
      if (infoEntry.showRelation == true) {
        infoEntry.showRelation = false;
        $('.relation').slideUp(200);
      } else {
        infoEntry.showRelation = true;
        $('.relation').slideDown(200);
      }
    };
    // 证件有效期不得早于现在
    infoEntry.minStartDate = new Date(new Date().getTime() + 1 * 24 * 60 * 60 * 1000);

    infoEntry.maxEndDate = new Date('2099-12-31');
    // 日期选择回调
    infoEntry.certExpireDateCallback = function(val) {
      if (val) {
        infoEntry.certExpireDate = val;
      }
    };
    // 日期选择回调
    infoEntry.birthdayCallback = function(val) {
      if (val) {
        infoEntry.birthday = val;
      }
    };
    /****** 事件监听 ******/
    // 监听投保人国籍变化，改变证件类型
    $scope.$watch('infoEntry.insureNationality', function(newValue) {
      if (newValue) {
        infoEntry.certTypes = infoEntry.getCertTypesByNationality(newValue);
        infoEntry.certType = infoEntry.certTypes[0].value;
      }
    });

    // 监听投保人证件类型变化
    // $scope.$watch('infoEntry.certType', function(newValue) {
    //   if (newValue) {
    //     infoEntry.certNo = '';
    //   }
    // });

    // 监听投保人证件号变化，如果为身份证，自动带出性别生日
    $scope.$watch('infoEntry.certNo', function(newValue) {
      if (newValue) {
        if (infoEntry.certType && infoEntry.certType == 'A') {
          if (!VALIDATION.idCheck(newValue)) {
            return;
          }
          infoEntry.gender = VALIDATION.getSex(newValue);
          infoEntry.genderDisabled = true;
          infoEntry.birthday = new Date(VALIDATION.getBirthday(newValue));
          infoEntry.age = infoEntry.getAge(VALIDATION.getBirthday(newValue));
          infoEntry.birthdayDisabled = true;
        }
      } else {
        infoEntry.genderDisabled = false;
        infoEntry.birthdayDisabled = false;
      }
    });

    // 监听投保人证件有效期
    $scope.$watch('infoEntry.certExpireType', function(newValue) {
      if (newValue == 1) {
        infoEntry.certExpireDate = '2099-12-31';
      } else if (newValue == 2) {
        infoEntry.certExpireDate = new Date(VALIDATION.dateFormat(infoEntry.certExpireDate));
      }
    });
    /****** 事件监听结束 ******/
    // 根据国籍代码获取证件类型数据
    infoEntry.getCertTypesByNationality = function(nationality) {
      var certTypes = [];
      if (!nationality) {
        certTypes = [{
          label: '身份证',
          value: 'A'
        }];
      }

      if (nationality == '0156') {
        certTypes = [{
          label: '身份证',
          value: 'A'
        }];
      } else if (nationality == '0999') {
        certTypes = [{
          label: '护照',
          value: 'I'
        }];
      } else {
        certTypes = [{
          label: '港澳台通行证',
          value: 'G'
        }];
      }
      return certTypes;
    };
    infoEntry.getAge = function(str) {
      var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
      if (r == null) {
        return false;
      }
      var date = new Date(r[1], r[3] - 1, r[4]);
      if (date.getFullYear() == r[1] && (date.getMonth() + 1) == r[3] && date.getDate() == r[4]) {
        var Y = new Date().getFullYear();
        return Y - r[1];
      }
      return 0;
    };
    // 提交信息
    infoEntry.submit = function() {
      console.log("提交成功");
    }
  })